'use strict';
let user = {}
export default class extends think.controller.base {
    async __before(action) {
        //登录、注册不验证token
        if (this.http.action === 'login' || this.http.action === 'register') {
            return;
        }
        //获取http-header token
        let userToken = this.http.header("token");
        //调用tokenservice中间件
        let tokenService = think.service("token");
        let tokenServiceInstance = new tokenService();
        //验证token
        let verifyTokenResult = await tokenServiceInstance.verifyToken(userToken);
        //服务器错误时
        if (verifyTokenResult === "fail") {
            this.fail("TOKEN_INVALID")
        }
        //获取用户信息
        user = verifyTokenResult.userInfo;
        //写入新token
        // this.http.header("token", newToken);
    }
    userInfo() {
        return user;
    }
}
